/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;
import dao.SanPham_dao;
import java.util.Hashtable;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileUploadBase.FileUploadIOException;
import org.apache.commons.fileupload.FileItem;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.io.File;
import java.util.ArrayList;
import javax.servlet.Servlet;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import pojo.KhachHang_pojo;
import pojo.SanPham_pojo;

/**
 *
 * @author Rocker000
 */
public class sanpham_controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            HttpSession session = request.getSession();
            int sosanphamtrentrang =5;
            String action = request.getParameter("action");
            if(action.equals("hienthidanhsachtheoloaisanpham"))
            {
                String madanhmuc = request.getParameter("madanhmuc");
                out.println("abc");
                int trang = 1;                
                if(request.getParameter("trang")!=null){trang = Integer.parseInt(request.getParameter("trang"));}
                String maloai = request.getParameter("maloaisanpham");
                ArrayList<SanPham_pojo> ds = new ArrayList<SanPham_pojo>();
                ds = SanPham_dao.LaySanPhamTheoMaLoai(maloai);
                
                ArrayList<SanPham_pojo> ds_theophantrang = new ArrayList<SanPham_pojo>();
                
                int sotrang = ds.size()/sosanphamtrentrang;                
                int sodu = ds.size()%sosanphamtrentrang;                
                if(sodu!=0)
                    sotrang = sotrang +1;
                
                int batdau = (trang-1)*sosanphamtrentrang;
                for(int i =batdau;i<batdau + sosanphamtrentrang;i++)
                {
                    if(i<ds.size())
                        ds_theophantrang.add(ds.get(i));
                }
                request.setAttribute("sotrang",sotrang);
                request.setAttribute("madanhmuc",madanhmuc);
                request.setAttribute("maloai",maloai);
                request.setAttribute("dssanphamphantrang", ds_theophantrang);
                request.setAttribute("dssanpham", ds);
                request.setAttribute("trang",trang);
                request.getRequestDispatcher("/danhsachsanpham.jsp").forward(request, response);                
            }
            
            if(action.equals("timkiemsanpham"))
            {
                String keyword = "";
                if(request.getParameter("txtkey")!=null)
                    keyword = request.getParameter("txtkey");
                
                String loai = "";
                if(request.getParameter("cbloaisanpham")!=null)
                    loai=request.getParameter("cbloaisanpham"); 
//                out.println(loai +"/"+ keyword);
                
                
                ArrayList<SanPham_pojo> dssanphamtimthay = new ArrayList<SanPham_pojo>();
                ArrayList<SanPham_pojo> dssanphamtimthayphantrang = new ArrayList<SanPham_pojo>();
                if(loai.equals("none"))
                {                   
                    dssanphamtimthay = SanPham_dao.TimKiemTheoKey(keyword);
                    out.println("none");
                }
                else
                {
                    dssanphamtimthay = SanPham_dao.TimKiemTheoKeyVaLoai(keyword,loai);
                    out.println("dont");
                     
                }
                int sosanphamtimthay =dssanphamtimthay.size();
                out.println("so san pham tim thay"+sosanphamtimthay);
               
                int trang = 1;
                if(request.getParameter("trang")!=null){trang=Integer.parseInt(request.getParameter("trang"));}
                int batdau = (trang-1)*sosanphamtrentrang;
                
                
                int sotrang = sosanphamtimthay/sosanphamtrentrang;
                
                int sodu = sosanphamtimthay%sosanphamtrentrang;
                
                if(sodu!=0)
                    sotrang = sotrang +1;
                
                out.println(sosanphamtimthay);
                out.println(sotrang);
                
                for(int i =batdau;i<batdau + sosanphamtrentrang;i++)
                {
                    if(i<dssanphamtimthay.size())
                        dssanphamtimthayphantrang.add(dssanphamtimthay.get(i));
                }
                request.setAttribute("loai",loai);
                request.setAttribute("keyword", keyword);
                request.setAttribute("sotrang",sotrang);
                request.setAttribute("dssanphamtimthayphantrang", dssanphamtimthayphantrang);
                request.setAttribute("dssanphamtimthay",dssanphamtimthay);
//                
//                out.println(trang);
//                out.println(sotrang);
                KhachHang_pojo kh = new KhachHang_pojo();
                if(session.getAttribute("KhachHang")!=null)
                    kh=(KhachHang_pojo)session.getAttribute("KhachHang");
                if(kh.getIsAdmin()==1)
                    request.getRequestDispatcher("/quanlysanpham.jsp").forward(request, response);
                else
                    request.getRequestDispatcher("/ketquatimkiem.jsp").forward(request, response);
            }
            
            if(action.equals("laysanphamtheothuonghieuvadanhmuc"))
            {
                String maloai=request.getParameter("maloaisanpham");
                String mahieu = request.getParameter("mathuonghieu");
                String madanhmuc = request.getParameter("madanhmuc");
                
                //out.println(mahieu+" "+madanhmuc);
                ArrayList<SanPham_pojo> dssanphamtimthay = new ArrayList<SanPham_pojo>();
                dssanphamtimthay = SanPham_dao.TimKiemTheoHieuVaDanhMuc(mahieu, madanhmuc);
                
                //out.println(dssanphamtimthay.size());
                ArrayList<SanPham_pojo> dssanphamtimthayphantrang = new ArrayList<SanPham_pojo>();
                
                int trang = 1;
                if(request.getParameter("trang")!=null){trang=Integer.parseInt(request.getParameter("trang"));}
                int batdau = (trang-1)*sosanphamtrentrang;
                
                int sosanphamtimthay = dssanphamtimthay.size();
                int sotrang = sosanphamtimthay/sosanphamtrentrang;                
                int sodu = sosanphamtimthay%sosanphamtrentrang;
                
                if(sodu!=0)
                    sotrang = sotrang +1;
                
                
                for(int i =batdau;i<batdau + sosanphamtrentrang;i++)
                {
                    if(i<dssanphamtimthay.size())
                        dssanphamtimthayphantrang.add(dssanphamtimthay.get(i));
                } 
                request.setAttribute("maloai", maloai);
                request.setAttribute("mathuonghieu",mahieu); 
                request.setAttribute("madanhmuc",madanhmuc);                
                request.setAttribute("sotrang",sotrang);
                request.setAttribute("dssanphamphantrang", dssanphamtimthayphantrang);
                request.setAttribute("dssanpham",dssanphamtimthay);
//                
//                out.println(trang);
//                out.println(sotrang);
                request.getRequestDispatcher("/danhsachsanpham.jsp").forward(request, response);
                
            }
            
            if(action.equals("capnhatsanpham"))
            {
                request.getRequestDispatcher("/Home.jsp").forward(request, response);
            }
            if(action.equals("laydanhsachsanphamtheogia"))
            {
                double heso = 1000000;
                String madanhmuc = request.getParameter("madanhmuc");
                
                String begin = String.valueOf(request.getParameter("begin"));
                String end =   String.valueOf(request.getParameter("end"));
                
                request.setAttribute("batdau",begin );
                request.setAttribute("ketthuc",end );
                
                double bg = heso*Double.parseDouble(begin);
                double e = heso*Double.parseDouble(end);
                
                out.println(begin + end);
                
                ArrayList<SanPham_pojo> dssanphamtimthay = new ArrayList<SanPham_pojo>();
                dssanphamtimthay = SanPham_dao.TimKiemTheoGia(bg, e);
                
                //out.println(dssanphamtimthay.size());
                ArrayList<SanPham_pojo> dssanphamtimthayphantrang = new ArrayList<SanPham_pojo>();
                
                int trang = 1;
                if(request.getParameter("trang")!=null){trang=Integer.parseInt(request.getParameter("trang"));}
                int batdau = (trang-1)*sosanphamtrentrang;
                
                int sosanphamtimthay = dssanphamtimthay.size();
                int sotrang = sosanphamtimthay/sosanphamtrentrang;                
                int sodu = sosanphamtimthay%sosanphamtrentrang;
                
                if(sodu!=0)
                    sotrang = sotrang +1;
                
                
                for(int i =batdau;i<batdau + sosanphamtrentrang;i++)
                {
                    if(i<dssanphamtimthay.size())
                        dssanphamtimthayphantrang.add(dssanphamtimthay.get(i));
                } 
                
                
                request.setAttribute("madanhmuc",madanhmuc);                
                request.setAttribute("sotrang",sotrang);
                request.setAttribute("dssanphamphantrang", dssanphamtimthayphantrang);
                request.setAttribute("dssanpham",dssanphamtimthay);
//                
//                out.println(trang);
//                out.println(sotrang);
                request.getRequestDispatcher("/danhsachsanpham.jsp").forward(request, response);
                
            }
            
                       
        } 
        finally 
        {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
